<?php
App::uses('AppController', 'Controller');
/**
 * ImportServices Controller
 *
 * @property ImportService $ImportService
 * @property PaginatorComponent $Paginator
 * @property SessionComponent $Session
 */
class ImportServicesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator', 'Session');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->ImportService->recursive = 0;
		$this->set('importServices', $this->Paginator->paginate());
	}
	public function import($id){
		$import =  $this->ImportService->find('all',array('conditions'=>array('services_id'=>$id)));
		$this->set(compact('import'));
		//debug($import);
	}
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->ImportService->exists($id)) {
			throw new NotFoundException(__('Invalid import service'));
		}
		$options = array('conditions' => array('ImportService.' . $this->ImportService->primaryKey => $id));
		$this->set('importService', $this->ImportService->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
	//debug($this->request->data);
	$id = $this->params['id'];
	$ser =  $this->ImportService->find('all',array('conditions'=>array('services_id'=>$this->params['id'])));
	$this->set(compact('ser','id'));
	$this->set('saved', false);
	$data = array();
	//$this->redirect(array('controller'=>'services','action' => 'index'));
	
		if ($this->request->is('post')) {
			$price = str_replace(',', '', $this->request->data['ImportService']['price']);
	
			$data['ImportService']['services_id']= $this->request->data['ImportService']['services_id'];
			$data['ImportService']['time']= $this->request->data['ImportService']['time'];
			$data['ImportService']['note']= $this->request->data['ImportService']['note'];
			$data['ImportService']['count']= $this->request->data['ImportService']['count'];
			$data['ImportService']['price']= $price;
			$this->ImportService->create();
			if ($this->ImportService->save($data)) {
				$this->set('saved', true);
			} else {
				//$this->set('saved', 3);
//$this->Session->setFlash(__('The import service could not be saved. Please, try again.'));
			}
		}
	
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->ImportService->exists($id)) {
			throw new NotFoundException(__('Invalid import service'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->ImportService->save($this->request->data)) {
				$this->Session->setFlash(__('The import service has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The import service could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('ImportService.' . $this->ImportService->primaryKey => $id));
			$this->request->data = $this->ImportService->find('first', $options);
		}
		$services = $this->ImportService->Service->find('list');
		$users = $this->ImportService->User->find('list');
		$this->set(compact('services', 'users'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->ImportService->id = $id;
		if (!$this->ImportService->exists()) {
			throw new NotFoundException(__('Invalid import service'));
		}
		$this->request->allowMethod('post', 'delete');
		if ($this->ImportService->delete()) {
			$this->Session->setFlash(__('The import service has been deleted.'));
		} else {
			$this->Session->setFlash(__('The import service could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
}
